﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 开单.Dao.基础;
using 开单.Model.基础;
using 开单.ComMethod;
using 开单.DB;

namespace 开单.Frm.基础
{
    public partial class FrmHpxx : Form
    {
        HpxxDao hpxxDao = new HpxxDao();
        HpxxEntity hpxxEntity = new HpxxEntity();
        DataTable dt = new DataTable();
        private static FrmHpxx Instance;
        private FrmHpxx()
        {
            InitializeComponent();
            this.uClistMoreHp.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreHp_ListEvent);
            RefeashDtv();
        }

        public static FrmHpxx GetInstance()
        {
            if (null == Instance || Instance.IsDisposed)
            {
                Instance = new FrmHpxx();
                //Instance.MdiParent = GlobalVar.frmMain;
            }
            Instance.BringToFront();
            return Instance;
        }

        private void RefeashDtv()
        {
            DataTable dtAll = hpxxDao.GetHpxxDt();
            dataGridView1.DataSource = dtAll;
            dtAll.Dispose();
        }

        void uClistMoreHp_ListEvent(开单.ComMethod.MyListItem myList)
        {
            TxtHp.Text = myList.TextField;
            TxtHp.Tag = myList.ValueField;
            uClistMoreHp.Visible = false;
            TxtHp.Focus();
        }

        private void toolStripQuery_Click(object sender, EventArgs e)
        {
            if (null != dt)
            {
                dt = hpxxDao.GetHpxxDtByHpName(TxtHp.Text.Trim());
                dataGridView1.DataSource = dt;
            }
        }

        private void TxtHp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt.Rows.Count)
                {
                    if (dt.Rows[0][1].ToString() == TxtHp.Text) { return; }
                }
                dt = hpxxDao.GetHpxxDtByHp_jp(TxtHp.Text.Trim());
                uClistMoreHp.addData(dt);
                uClistMoreHp.BringToFront();
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.RowCount == 0) { return; }
            if (e.RowIndex == -1) { return; }
            TxtHp.Tag = dataGridView1[0, e.RowIndex].Value;
            TxtHpName.Text = dataGridView1[1, e.RowIndex].Value.ToString();
            TxtHpDj.Text = dataGridView1[2, e.RowIndex].Value.ToString();
            TxtHpDw.Text = dataGridView1[3, e.RowIndex].Value.ToString();
            TxtHpGg.Text = dataGridView1[4, e.RowIndex].Value.ToString();
            TxtHpjp.Text = dataGridView1[5, e.RowIndex].Value.ToString();
            groupBox3.Visible = true;
            button2.Visible = false;
            button1.Visible = true;
            toolStripDel.Visible = true;
        }

        private void toolStripRefeash_Click(object sender, EventArgs e)
        {
            RefeashDtv();
            groupBox3.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定要修改吗？", "提示", MessageBoxButtons.YesNo,
                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            hpxxEntity.hp_code = Convert.ToInt32(TxtHp.Tag.ToString());
            hpxxEntity.hp_pm = TxtHpName.Text;
            hpxxEntity.hp_jp = TxtHpjp.Text;
            hpxxEntity.hp_dj = Convert.ToDecimal(TxtHpDj.Text);
            hpxxEntity.hp_dw = TxtHpDw.Text;
            hpxxEntity.hp_gg = TxtHpGg.Text;
            hpxxEntity.hp_jp = TxtHpjp.Text;
            hpxxDao.UpdataHpxxByHp_code(hpxxEntity);
            RefeashDtv();
            MessageBox.Show("修改成功");
            IntityControl();
            groupBox3.Visible = false;
        }

        private void TxtHpName_TextChanged(object sender, EventArgs e)
        {
            this.TxtHpjp.Text = GetPYString.GetPYStr(TxtHpName.Text);
        }

        private void toolStripClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            groupBox3.Visible = true;
            button2.Visible = true;
            button1.Visible = false;
            TxtHpName.Focus();
            IntityControl();
        }

        private void IntityControl()
        {
            TxtHp.Tag = null;
            TxtHpName.Text = "";
            TxtHpjp.Text = "";
            TxtHpDj.Text = "";
            TxtHpDw.Text = "";
            TxtHpGg.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(!(IsOkToAdd())){return;};
            if (!(MessageBox.Show("确定要增加吗？", "提示", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            //hpxxEntity.hp_code = Convert.ToInt32(TxtHp.Tag.ToString());
            hpxxEntity.hp_pm = TxtHpName.Text;
            hpxxEntity.hp_jp = TxtHpjp.Text;
            hpxxEntity.hp_dj = Convert.ToDecimal(TxtHpDj.Text);
            hpxxEntity.hp_dw = TxtHpDw.Text;
            hpxxEntity.hp_gg = TxtHpGg.Text;
            hpxxEntity.hp_jp = TxtHpjp.Text;
            hpxxDao.AddHpxx(hpxxEntity);
            RefeashDtv();
            MessageBox.Show("修改成功");
            IntityControl();
        }

        private bool IsOkToAdd()
        {
            if (TxtHpName.Text.Trim() == "") { MessageBox.Show("请输入货品名"); TxtHpName.Focus(); return false; }
            if (TxtHpDj.Text.Trim() == "") { MessageBox.Show("请输入单价"); TxtHpDj.Focus(); return false; }
            if (TxtHpDw.Text.Trim() == "") { MessageBox.Show("请输入单位"); TxtHpDw.Focus(); return false; }
            if (TxtHpGg.Text.Trim() == "") { MessageBox.Show("请输入规格"); TxtHpGg.Focus(); return false; }
            try
            {
                hpxxEntity.hp_dj = Convert.ToDecimal(TxtHpDj.Text);
            }
            catch
            {
                MessageBox.Show("单价必须为数字");
                TxtHpDj.Text = "";
                TxtHpDj.Focus();
                return false;
            }
            return true;
            
        }

        private void toolStripDel_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            if (dataGridView1.RowCount == 0 || dataGridView1.CurrentCell.RowIndex == -1) { return; }
            hpxxDao.DelHpxx(TxtHp.Tag);
            RefeashDtv();
            MessageBox.Show("删除成功");
            groupBox3.Visible = false;
            IntityControl();
        }

    }
}
